/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.stateful;

import entity.CustomerEntity;
import entity.PurchaseOrderEntity;
import entity.SalesQuotationEntity;
import java.util.ArrayList;
import java.util.Hashtable;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author edd
 */
@Stateful
@LocalBean
public class PurchaseOrderSessionBean {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext
    EntityManager em;
    PurchaseOrderEntity purchaseOrder;
    SalesQuotationEntity salesQuotation;
    CustomerEntity customer;
    
    public PurchaseOrderSessionBean() {
        
    }
    
    /*public void addPurchaseOrder(Long salesQuotationId, String settlementMode) throws Exception {
        
        salesQuotation = em.find(SalesQuotationEntity.class, salesQuotationId);
        if(salesQuotation == null)
            throw new Exception("Sales Quotation does not exist.");
        else {
            purchaseOrder = new PurchaseOrderEntity(settlementMode);
            purchaseOrder.setSalesQuotation(salesQuotation);
            
            //check for customer account
            customer = salesQuotation.getCustomer();
            if(customer == null) {
                //convert into customer
            }   
            em.persist(purchaseOrder);
        }
    }*/
    
    /*public void editPurchaseOrder(Long purchaseOrderId, Hashtable<Long, Integer> productQuantity, 
            double price, int indicativeLeadTime, String settlementMode) throws Exception {
        
        purchaseOrder = em.find(PurchaseOrderEntity.class, purchaseOrderId);
        if(purchaseOrder == null)
            throw new Exception("Purchase Order does not exist.");
        else {
            //do not edit existing purchase order. archive the existing one and create a new purchase order
            deletePurchaseOrder(purchaseOrderId);
            //change in order requires a new sales quotation as stated in the proposal
            salesQuotation = new SalesQuotationEntity(productQuantity, price, indicativeLeadTime);
            //match to new sales quotation
            addPurchaseOrder(salesQuotation.getId(), settlementMode);
            em.persist(purchaseOrder);
        }  
    }*/
    
    public void deletePurchaseOrder(Long purchaseOrderId) throws Exception {
        
        purchaseOrder = em.find(PurchaseOrderEntity.class, purchaseOrderId);
        if(purchaseOrder == null)
            throw new Exception("Purchase Order does not exist.");
        else if(purchaseOrder.isArchived()) 
            throw new Exception("Purchase Order has already been deleted.");
        else {
            purchaseOrder.setArchived(true);
            em.persist(purchaseOrder);
        }
    }
    
    public ArrayList<PurchaseOrderEntity> viewPurchaseOrder() {
        
        Query query = em.createQuery("SELECT p FROM PurchaseOrder p WHERE p.archived = :notArchived "
                + "ORDER BY p.id desc");
        query.setParameter("notArchived", "false");
        
        ArrayList listPurchaseOrder = new ArrayList<PurchaseOrderEntity>();
        
        for(Object o : query.getResultList()) {
            PurchaseOrderEntity p = (PurchaseOrderEntity) o;
            listPurchaseOrder.add(p);
        }
        return listPurchaseOrder;
    }
    
    
}
